<?php
class CFile  {
	
	function __cunstruct() {
	
	}
	
	static public function lineReadFile($path) {
		$myfile = file ( $path );
		foreach ( $myfile as $value ) {
			$data .= $value;
		}
		if ($data == '') {
			return "读取不了任何数据！";
		} else {
			return $data;
		}
	}
	
	static public function lineEchoData($path) {
		echo $this->readToWriteFile ( $path );
	}
	
	static public function allReadFile($path) {
		@$data = file_get_contents ( $path );
		if ($data) {
			return $data;
		} else {
			//rror::addError ( "can not read data" );
			return false;
		}
	}
	
	static public function allEchoFile($path) {
		echo CFile::allReadFile ( $path );
	}
	
	static public function openAndWrite($oldPath, $newPath) {
		$data = CFile::allReadFile ( $oldPath );
		if(!$data){
			return false;
		};
		$fp = fopen ( $newPath, "w" );
		if (! $fp) {
			//MObject::showMsg ( "读取不了任何数据！" );
			return false;
		} else {
			fwrite ( $fp, $data );
			fclose ( $fp );
			return true;
		}
	}
	
	static public function openAndWrite2($oldPath, $newPath) {
		$thisData = self::oldReadFile ( $oldPath );
		self::onlyWrite ( $newPath, $thisData );
	}
	
	static public function onlyWrite($newPath, $data) {
		$fp = fopen ( $newPath, "w" );
		if (! $fp) {
			echo "读取不了任何数据！" ;
			return false;
		} else {
			fwrite ( $fp, $data );
			fclose ( $fp );
			return true;
		}
	}
	
	static public function GetWebContent($host, $method, $path, $str, $sessid) {
		$fp = fsockopen ( $host, 80 );
		
		if (! $fp) {
			$this->setError ( "读取不了任何数据！" );
			return $this->error;
		}
		
		if ($method == "GET") {
			$path .= '?' . $str;
		}
		
		fputs ( $fp, "$method $path HTTP/1.1\n" );
		fputs ( $fp, "Host: $host\n" );
		
		if (! empty ( $sessid )) {
			fputs ( $fp, "Cookie: $sessid; path=/;\n" );
		}
		fputs ( $fp, "Content-Type: application/x-www-form-urlencoded\n" );
		
		if ($method == "POST") {
			fputs ( $fp, "Content-Length: " . strlen ( $str ) . "\n" );
		}
		fputs ( $fp, "Connection: close\n\n" );
		
		if ($method == "POST") {
			fputs ( $fp, $str . "\n" );
		}
		
		while ( ! feof ( $fp ) ) {
			$response .= fgets ( $fp, 1024 );
		}
		
		fclose ( $fp );
		return $response;
	}
	
	static public function oldReadFile($filename) {
		$handle = fopen ( $filename, "rb" );
		$contents = fread ( $handle, filesize ( $filename ) );
		fclose ( $handle );
		return $contents;
	}
	
	static public function wp_remote_fopen($uri) {
		$timeout = 10;
		$parsed_url = @parse_url ( $uri );
		
		if (! $parsed_url || ! is_array ( $parsed_url ))
			return false;
		
		if (! isset ( $parsed_url ['scheme'] ) || ! in_array ( $parsed_url ['scheme'], array ('http', 'https' ) ))
			$uri = 'http://' . $uri;
		
		if (ini_get ( 'allow_url_fopen' )) {
			$fp = @fopen ( $uri, 'r' );
			if (! $fp)
				return false;
				
			//stream_set_timeout($fp, $timeout); // Requires php 4.3
			$linea = '';
			while ( $remote_read = fread ( $fp, 4096 ) )
				$linea .= $remote_read;
			fclose ( $fp );
			return $linea;
		} else if (function_exists ( 'curl_init' )) {
			$handle = curl_init ();
			curl_setopt ( $handle, CURLOPT_URL, $uri );
			curl_setopt ( $handle, CURLOPT_CONNECTTIMEOUT, 1 );
			curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, 1 );
			curl_setopt ( $handle, CURLOPT_TIMEOUT, $timeout );
			$buffer = curl_exec ( $handle );
			curl_close ( $handle );
			return $buffer;
		} else {
			return false;
		}
	}
	
	static public function deleteOneFile($path, $error = "删除失败!") {
		$isTrue = @unlink ( $path );
		if ($isTrue) {
			return true;
		} else {
			return false;
			;
		}
	}
	
	static public function deleteFileArray($pathArray) {
		
		foreach ( $pathArray as $path ) {
			if(file_exists($path)){
				@unlink ( $path );
			}
		}
	}
	
	static public function createFolder($path) {
		$isTrue = false;
		if (! file_exists ( $path )) {
			self::createFolder ( dirname ( $path ) );
			$isTrue = mkdir ( $path, 0777 );
		}
		return $isTrue;
	}
	
	//刪除文件但不刪除文件夾
	static public function delDirFile2($dir) {
		@$handle = opendir ( $dir );
		if ($handle) {
			while ( false !== ($file = readdir ( $handle )) ) {
				if ($file == "." || $file == "..") {
					continue;
				}
				@unlink ( $dir . $file );
			}
		}
	}
	
	//刪除文件并刪除文件夾
	static public function delDirFile($dir) {
		$dh = @opendir ( $dir );
		
		while ( true ) {
			@$file = readdir ( $dh );
			if ($file == "") {
				break;
			}
			if ($file != "." && $file != "..") {
				$fullpath = $dir . "/" . $file;
				if (! is_dir ( $fullpath )) {
					@unlink ( $fullpath );
				} else {
					self::delDirFile ( $fullpath );
				}
			}
		}
		
		@closedir ( $dh );
		
		if (@rmdir ( $dir )) {
			return true;
		} else {
			return false;
		}
	}

	
	static public function copyAndUnlink($source, $dest) {
		if (file_exists ( $source )) {
			self::copyFile ( $source, $dest );
			return @unlink ( $source );
		}
		return false;
	}
	
	static public function copyFile($source, $dest) {
		$isTrue = false;
		$newPath = preg_replace ( "/\/[^\/]+\.[a-zA-Z0-9]+$/", "", $dest );
		if (! @opendir ( $newPath )) {
			$isTrue = self::createFolder ( $newPath );
		}
		copy ( $source, $dest );
		return $isTrue;
	}
	
	static public function getFileName($path) {
		$pathArray = explode ( "/", $path );
		return $pathArray [count ( $pathArray ) - 1];
	}
	
	static public function getFileType($path) {
		$fileArray = explode ( ".", $path );
		return $fileArray [count ( $fileArray ) - 1];
	}
	
	static public function getPostRemote($argv,$host,$uri,$refer=''){
		
		$allContent='';
		
		$flag = 0;
		//构造要post的字符串 
		foreach ($argv as $key=>$value) { 
		    if ($flag!=0) {
		        $params .= "&"; 
		        $flag = 1; 
		    } 
		    $params.= $key."="; $params.= urlencode($value); 
		    $flag = 1; 
		} 
	    $length = strlen($params);
	     //创建socket连接 
	    $fp = fsockopen($host,80,$errno,$errstr,10) or exit($errstr."--->".$errno); 
	    //构造post请求的头 
	    $header = "POST $uri HTTP/1.1\r\n"; 
	    $header .= "Host:$host\r\n"; 
	    $header .= "Referer:$refer\r\n";
	    $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
	    $header .= "Content-Length: ".$length."\r\n"; 
	    $header .= "Connection: Close\r\n\r\n";
	    //添加post的字符串 
	    $header .= $params."\r\n"; 
	    //发送post的数据 
	    fputs($fp,$header); 
	    $inheader = 1; 
	    while (!feof($fp)) {
	        $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据 
	        if ($inheader && ($line == "\n" || $line == "\r\n")) {
	             $inheader = 0; 
	        } 
	        if ($inheader == 0) { 
	            $allContent.=$line;
	        } 
	    } 
		fclose($fp); 
		
		return $allContent;
		
	}

}
